import {useAtom} from 'jotai'
import {mapAtom} from '../components/SocketManager.jsx'
import * as THREE from 'three'

export const useGrid = () => {

  const [map, setMap] = useAtom(mapAtom)


  // console.log(map)
  const v3ToGrid = (v3) => {
    const {girdSpace} = map
    return [
      Math.floor(v3.x * girdSpace),
      Math.floor(v3.z * girdSpace),
    ]
  }

  const gridToV3 = (pos, width = 1, height = 1) => {
    const {girdSpace} = map
    return new THREE.Vector3(
      width / girdSpace / 2 + pos[0] / girdSpace,
      0,
      height / girdSpace / 2 + pos[1] / girdSpace,
    )
  }

  return {
    v3ToGrid,
    gridToV3
  }
}
